package com.zego.wawaji.third.zego;

import com.zego.wawaji.wawaji_client.App;
import com.zego.wawaji.wawaji_client.util.PreferenceUtil;
import com.zego.zegoliveroom.ZegoLiveRoom;
import com.zego.zegoliveroom.constants.ZegoAvConfig;

/**
 * zego相关管理类
 * Created by Su on 2017/12/6.
 */
public class ZegoMgr {
    private static ZegoMgr sInstance = null;

    private ZegoMgr() {
        mZegoLiveRoom = new ZegoLiveRoom();
    }

    public static ZegoMgr getInstance() {
        if (sInstance == null) {
            synchronized (ZegoMgr.class) {
                if (sInstance == null) {
                    sInstance = new ZegoMgr();
                }
            }
        }
        return sInstance;
    }

    // ========================= SDK 相关 ===================================
    private ZegoLiveRoom mZegoLiveRoom = null;
    private ZegoAvConfig mZegoAvConfig = null;
    private String mUserID = null;

    // Zego SDK是否初始化成功
    public static boolean isInitSuc = false;

    /**
     * 初始化sdk.
     */
    public void initSDK() {
        long appID = 2123036530;
        byte[] signKey = {
                (byte) 0x80, (byte) 0xd5, (byte) 0x16, (byte) 0xd1, (byte) 0xdd, (byte) 0xb5, (byte) 0xd0, (byte) 0x19,
                (byte) 0xd5, (byte) 0x58, (byte) 0x3f, (byte) 0xac, (byte) 0xb0, (byte) 0xfc, (byte) 0xb5, (byte) 0x5c,
                (byte) 0x38, (byte) 0xda, (byte) 0x89, (byte) 0xca, (byte) 0x84, (byte) 0xd5, (byte) 0xd2, (byte) 0x39,
                (byte) 0x4c, (byte) 0xb0, (byte) 0xf7, (byte) 0xe2, (byte) 0xb7, (byte) 0x10, (byte) 0x72, (byte) 0x97};

        ZegoLiveRoom.setTestEnv(true);      // 配置测试环境
        init(appID, signKey);
    }

    private void init(long appID, byte[] signKey) {
        initUserInfo();

        // 初始化sdk
        isInitSuc = mZegoLiveRoom.initSDK(appID, signKey, App.sApplicationContext);
        if (!isInitSuc) {
            // sdk初始化失败
        } else {
            // 初始化设置级别为"High"
            mZegoAvConfig = new ZegoAvConfig(ZegoAvConfig.Level.High);
            mZegoLiveRoom.setAVConfig(mZegoAvConfig);
        }
    }

    private void initUserInfo() {
        // 初始化用户信息
        mUserID = PreferenceUtil.getInstance().getUserID();
        String userName = PreferenceUtil.getInstance().getUserName();
        ZegoLiveRoom.setUser(mUserID, userName);
    }

    /**
     * 获取直播Room对象
     *
     * @return
     */
    public ZegoLiveRoom getZegoLiveRoom() {
        return mZegoLiveRoom;
    }

    /**
     * 退出释放SDK
     */
    public void releaseSDK() {
        isInitSuc = false;
        mZegoLiveRoom.unInitSDK();
    }


}
